package yanc.app

import grails.converters.JSON
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class BusinessController {
    def businessService
    static final Logger logger = LoggerFactory.getLogger(BusinessController.class)

    def getCom() {
        def id = "${params.id ?: ''}"
        println "id" + id
        try {
            Business branch = Business.findById(id)
            render(view: "show", model: [data: branch.comm])
        } catch (Exception e) {
            logger.error("CommController Exception " + e.message)
        }
    }

    def add() {
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def title = "${params.title ?: ''}"
        def imgUrl = "${params.imgUrl ?: ''}"
        def business = new Business()
        bindData(business, params, [exclude: ['page', 'pagesize', 'title', 'imgUrl']])
        try {
            businessService.add(business, title, imgUrl)
            def list = Business.list(max: pagesize, offset: (page - 1) * pagesize, sort: "modifyDate", order: "desc")
            def length = Business.count()
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
            render data as JSON
        } catch (Exception e) {
            logger.error("BusinessController add Exception" + e.message)
        }
    }

    def getAll() {
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def name = "${params.name ?: ''}"
        def status = "${params.status ?: ''}"
        def a = Business.createCriteria()
        def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
            and {
                if (!name.isEmpty()) {
                    ilike("name", "%${name}%")
                }
                if (!status.isEmpty()) {
                    if (status == 'true') {
                        eq("status", true)
                    } else {
                        eq("status", false)
                    }
                }
            }
            order("modifyDate", "desc")
        }
        def b = Business.createCriteria()
        def length = b.get {
            and {
                if (!name.isEmpty()) {
                    ilike("name", "%${name}%")
                }
                if (!status.isEmpty()) {
                    if (status == 'true') {
                        eq("status", true)
                    } else {
                        eq("status", false)
                    }
                }
            }
            projections {
                rowCount()
            }
        }
        if (list) {
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
        } else {
            data << [statuCode: "1", errMsg: "1"]
        }
        render data as JSON
    }

    def refresh() {
        def data = [:]
        def page = 1
        def pagesize = 20
        def id = "${params.id ?: ''}"
        def title = "${params.title ?: ''}"
        def imgUrl = "${params.imgUrl ?: ''}"
        def business = Business.findById(id)
        bindData(business, params, [exclude: ['id', 'title', 'imgUrl']])
        try {
            businessService.update(business, title, imgUrl)
            def list = Business.list(max: pagesize, offset: (page - 1) * pagesize, sort: "modifyDate", order: "desc")
            def length = Business.count()
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
            render data as JSON
        } catch (Exception e) {
            logger.error("BusinessController refresh Exception" + e.message)
        }
    }

    def del() {
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def id = "${params.id ?: ''}"
        try {
            businessService.del(id)
            def list = Business.list(max: pagesize, offset: (page - 1) * pagesize, sort: "modifyDate", order: "desc")
            def length = Business.count()
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
            render data as JSON
        } catch (Exception e) {
            logger.error("BusinessController del Exception" + e.message)
        }
    }
}
